package com.CampaignManager.shared;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@SuppressWarnings("serial")
public class PlayerDTO implements Serializable {

	public static String GROUP_NAME = "game-of-throne-and-blade-warmachine";
	public static int FORAGE_MILES = 20;
	public static double MILES_TO_LATLONG = 0.0001478;
	public static double MILES_PER_DAY = 50;
	public static double GOLD_PER_MILE_DELIVERY = .142;
	public static double GOLD_PER_MILE_ESCORT = .428;
	public static double GOLD_PER_BATTLE_POINT = 15;
	public static int GOLD_PER_LEVERAGE = 14;
	
	public enum Stat {
		Level,
		EspionageLevel,
		NarrationPoints			
	}
	
	public static Long getPointLimit(Long level) {
		return (level*5) + 10;
	}

	private String nickName;	
	private String emailAddress;
	private String characterName;
	private String characterDescription;
	private boolean admin = false;
	private String logoutUrl;
	private double originLatitude;
	private double originLongitude;
	private double destLatitude;
	private double destLongitude;
	private double milesToTravel;
	private Date moveStart;
	private boolean doneMapTutorial = false;
	private boolean doneJobTutorial = false;
	private boolean doneEspionageTutorial = false;
	private boolean doneHireTutorial = false;
	private Long espionageLevel = 0L;
	private String faction;
	private Long level = 0L;
	private Date lastForageDate;
	private boolean currentPlayer = false;
	private String imageURL;
	private Long narrationPoints = 0L;
	
	private List<ResourceAmountDTO> resources;
	public List<ResourceAmountDTO> getResources() {
		if (resources == null) resources = new ArrayList<ResourceAmountDTO>();
		return resources;
	}
	public ResourceAmountDTO getResource(String name) {
		ResourceAmountDTO ret = null;
		for (ResourceAmountDTO rad : this.getResources()) {
			if (rad.getResource().equalsIgnoreCase(name)) {
				ret = rad;
			}
		}
		if (ret == null) {
			ret = new ResourceAmountDTO();
			ret.setResource(name);
			ret.setAmount(0L);
			this.getResources().add(ret);
		}
		return ret;
	}
	
	private List<PlayerCityDTO> cities;	
	public List<PlayerCityDTO> getCities() {
		if (cities == null) cities = new ArrayList<PlayerCityDTO>();
		return cities;
	}
	public PlayerCityDTO getCity(String name) {
		PlayerCityDTO city = null;
		for (PlayerCityDTO pc : this.getCities()) {
			if (pc.getCity().equalsIgnoreCase(name)) {
				city = pc;
			}
		}
		if (city == null) {
			city = new PlayerCityDTO();
			city.setCity(name);
			city.setFavor(0L);
			city.setLeverage(0L);
			this.getCities().add(city);
		}
		return city;
	}
	
	private List<UnitDTO> units = new ArrayList<UnitDTO>();
	public List<UnitDTO> getUnits() {
		return units;
	}
	
	private List<JobDTO> jobs;	
	public List<JobDTO> getJobs() {
		if (jobs == null) jobs = new ArrayList<JobDTO>();
		return jobs;
	}

	public Long getNarrationPoints() {
		return narrationPoints;
	}
	public void setNarrationPoints(Long narrationPoints) {
		this.narrationPoints = narrationPoints;
	}
	public String getImageURL() {
		return imageURL;
	}
	public void setImageURL(String imageURL) {
		this.imageURL = imageURL;
	}
	public String getNickName() {
		return nickName;
	}
	public void setNickName(String nickName) {
		this.nickName = nickName;
	}
	public boolean isCurrentPlayer() {
		return currentPlayer;
	}
	public void setCurrentPlayer(boolean currentPlayer) {
		this.currentPlayer = currentPlayer;
	}
	public Date getLastForageDate() {
		return lastForageDate;
	}
	public void setLastForageDate(Date lastForageDate) {
		this.lastForageDate = lastForageDate;
	}
	public Long getLevel() {
		return level;
	}
	public void setLevel(Long level) {
		this.level = level;
	}
	public String getFaction() {
		return faction;
	}
	public void setFaction(String faction) {
		this.faction = faction;
	}
	public Long getEspionageLevel() {
		return espionageLevel;
	}
	public void setEspionageLevel(Long espionageLevel) {
		this.espionageLevel = espionageLevel;
	}
	public String getEmailAddress() {
		return emailAddress;
	}
	public double getDestLatitude() {
		return destLatitude;
	}
	public void setDestLatitude(double destLatitude) {
		this.destLatitude = destLatitude;
	}
	public double getDestLongitude() {
		return destLongitude;
	}
	public void setDestLongitude(double destLongitude) {
		this.destLongitude = destLongitude;
	}
	public double getMilesToTravel() {
		return milesToTravel;
	}
	public void setMilesToTravel(double milesToTravel) {
		this.milesToTravel = milesToTravel;
	}
	public Date getMoveStart() {
		return moveStart;
	}
	public void setMoveStart(Date moveStart) {
		this.moveStart = moveStart;
	}
	public void setEmailAddress(String emailAddress) {
		this.emailAddress = emailAddress;
	}
	public String getCharacterName() {
		return characterName;
	}
	public void setCharacterName(String characterName) {
		this.characterName = characterName;
	}
	public String getCharacterDescription() {
		return characterDescription;
	}
	public void setCharacterDescription(String characterDescription) {
		this.characterDescription = characterDescription;
	}
	public boolean isAdmin() {
		return admin;
	}
	public void setAdmin(boolean admin) {
		this.admin = admin;
	}
	public String getLogoutUrl() {
		return logoutUrl;
	}
	public void setLogoutUrl(String logoutUrl) {
		this.logoutUrl = logoutUrl;
	}
	public double getOriginLatitude() {
		return originLatitude;
	}
	public void setOriginLatitude(double originLatitude) {
		this.originLatitude = originLatitude;
	}
	public double getOriginLongitude() {
		return originLongitude;
	}
	public void setOriginLongitude(double originLongitude) {
		this.originLongitude = originLongitude;
	}
	public boolean isDoneMapTutorial() {
		return doneMapTutorial;
	}
	public void setDoneMapTutorial(boolean doneMapTutorial) {
		this.doneMapTutorial = doneMapTutorial;
	}	
	public boolean isDoneJobTutorial() {
		return doneJobTutorial;
	}
	public void setDoneJobTutorial(boolean doneJobTutorial) {
		this.doneJobTutorial = doneJobTutorial;
	}
	public boolean isDoneEspionageTutorial() {
		return doneEspionageTutorial;
	}
	public void setDoneEspionageTutorial(boolean doneEspionageTutorial) {
		this.doneEspionageTutorial = doneEspionageTutorial;
	}
	public boolean isDoneHireTutorial() {
		return doneHireTutorial;
	}
	public void setDoneHireTutorial(boolean doneHireTutorial) {
		this.doneHireTutorial = doneHireTutorial;
	}
	
	public Long getArmySize() {
		
		long total = 0;
		
		for (UnitDTO unit : this.getUnits()) {
			total += unit.getPoints();
		}
    	
    	return total;
	}
	
	public boolean canPlayerAffordThis(List<ResourceAmountDTO> cost) {
		
		for (ResourceAmountDTO rac : cost) {
			boolean haveThisResource = false;
			ResourceAmountDTO playerRac = this.getResource(rac.getResource());
			if (playerRac.getAmount() >= rac.getAmount()) {
				haveThisResource = true;
			}
			if (!haveThisResource) return false;
		}		
	
		return true;		
	}
	
	public List<JobDTO> getOngoingJobs() {
		List<JobDTO> jobs = new ArrayList<JobDTO>();
		for (JobDTO job : this.getJobs()) {
			if ((job.getStatus().equals("Pending") || 
				 job.getStatus().equals("Incomplete")) &&
				(job.getJobType().equals("Delivery") ||
				 job.getJobType().equals("Escort") ||
				 job.getJobType().equals("Battle"))) {
				jobs.add(job);
			}
		}
		return jobs;
	}
	public List<JobDTO> getOngoingEspionage() {
		List<JobDTO> jobs = new ArrayList<JobDTO>();
		for (JobDTO job : this.getJobs()) {
			if ((job.getStatus().equals("Pending") || 
				 job.getStatus().equals("Incomplete")) &&
				(job.getJobType().equals("Warmonger") ||
				 job.getJobType().equals("Surveillance") ||
				 job.getJobType().equals("Bribe"))) {
				jobs.add(job);
			}
		}
		return jobs;
	}
}
